﻿namespace System.Windows.Controls
{
    using System.Windows.Automation.Peers;
    using System.Windows.Automation.Provider;

    /// <summary>
    /// 按钮的的扩展
    /// </summary>
    public static class ButtonExtension
    {
        /// <summary>
        /// 点击按钮
        /// </summary>
        /// <param name="clickBtn"></param>
        public static void PerfomClick(this Button clickBtn)
        {
            //这里必须按判断是否处于Enable状态
            if (clickBtn != null && clickBtn.IsEnabled)
            {
                //通过代码点击按钮
                var peer = new ButtonAutomationPeer(clickBtn);
                var invokeProv = peer.GetPattern(PatternInterface.Invoke) as IInvokeProvider;
                if (invokeProv != null)
                {
                    invokeProv.Invoke();
                    //调用后同时设置焦点为按钮
                    //clickBtn.Focus();
                }
            }
        }

    }
}
